package com.sky.service.common.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.common.SetmealDishMapper;
import com.sky.mapper.common.SetmealMapper;
import com.sky.pojo.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.common.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult findByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page1 = setmealMapper.findByPage(setmealPageQueryDTO.getName(), setmealPageQueryDTO.getCategoryId(), setmealPageQueryDTO.getStatus());
        return new PageResult(page1.getTotal(), page1.getResult());
    }

    @Override
    @Transactional
    public Result<String> save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.save(setmeal);
        setmealDTO.getSetmealDishes().forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealDishMapper.BatchSave(setmealDTO.getSetmealDishes());
        return Result.success();
    }


    @Override
    public SetmealVO findById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        setmealVO = setmealMapper.findById(id);
        return setmealVO;
    }

    @Override
    public Result<String> update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        setmealDishMapper.deleteById(setmealDTO.getId());
        setmealDTO.getSetmealDishes().forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealDishMapper.BatchSave(setmealDTO.getSetmealDishes());
        return Result.success();
    }


    @Override
    public Result<String> deleteByIds(Long[] ids) {

        List.of(ids).forEach(id -> {
            SetmealVO setmeal = setmealMapper.findById(id);
            if(StatusConstant.ENABLE.equals(setmeal.getStatus())){
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        setmealMapper.deleteByIds(ids);
        setmealDishMapper.deleteByIds(ids);

        return Result.success();
    }

    @Override
    public Result<String> startOrStop(Integer status, Long id) {
        setmealMapper.startOrStop(status, id);
        return Result.success();

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
